package com.popay.gateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@Component
@EnableDiscoveryClient
//@EntityScan("com.popay.*.entity")
@SpringBootApplication
public class PopayGatewayApplication  {

    public static void main(String[] args)throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(PopayGatewayApplication.class, args);
        Environment env = application.getEnvironment();
        String contextPath = env.getProperty("server.servlet.context-path");
        if (contextPath == null) {
            contextPath = "";
        }
        String agreement = env.getProperty("server.ssl.key-store");
        if (agreement == null) {
            agreement = "http";
        } else {
            agreement = "https";
        }
        log.info("\n---------------------------------------------------------\n" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\t{}://localhost:{}{}\n\t" +
                        "External: \t{}://{}:{}{}\n\t"+
                        "knife4j Api Doc: \t{}://{}:{}{}/doc.html\n"+
                        "\n---------------------------------------------------------\n",
                env.getProperty("spring.application.name"),
                agreement,env.getProperty("server.port"),contextPath,
                agreement,InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"),contextPath,
                agreement,InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"),contextPath);
    }



    /**
     * 基本的转发
     * 当访问http://localhost:8080/jd
     * 转发到http://jd.com
     * @param builder
     * @return
     */
//    @Bean
//    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
//
//
//        return builder.routes()
//                .route("crm", r -> r.path("/app")
//                        .uri("http://localhost:9099/index"))
//                .build();
//    }
//    @Bean
//    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
//
//        RouteLocatorBuilder.Builder routes=routeLocatorBuilder.routes();
//
//        routes.route("path_route_atguigu",
//                r ->r.path("/guonei")
//                        .uri("http://news.baidu.com/guonei")).build();
//
//        return routes.build();
//    }

}
